//
//  ZYXUIButtonViewController.m
//  OCUIBasis
//  UIButton的使用
//

#import "ZYXUIButtonViewController.h"

@interface ZYXUIButtonViewController ()

/**
 *  图片按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *head;

/**
 *  控制图片向左移动
 */
- (IBAction)left;
- (IBAction)top;
- (IBAction)right;
- (IBAction)bottom;

/**
 *  图片放大
 */
- (IBAction)big;

/**
 *  图片缩小
 */
- (IBAction)small;


/**
 *  控制图片移动
 *
 *  @param sender 按钮
 */
- (IBAction)move:(UIButton *)sender;

/**
 *  放大缩小图片
 *
 *  @param sender 按钮
 */
- (IBAction)scale:(UIButton *)sender;

/**
 *  移动缩放
 *
 *  @param sender 按钮
 */
- (IBAction)moveAndBig:(UIButton *)sender;


- (IBAction)up;
- (IBAction)leftRotate;
- (IBAction)plus;




@end

@implementation ZYXUIButtonViewController



- (IBAction)left
{
    CGRect tempFrame = self.head.frame;
    tempFrame.origin.x -= 10;
    self.head.frame = tempFrame;
}

- (IBAction)top {
    CGRect tempFrame = self.head.frame;
    tempFrame.origin.y -= 10;
    self.head.frame = tempFrame;
}

- (IBAction)right {
    CGRect tempFrame = self.head.frame;
    tempFrame.origin.x += 10;
    self.head.frame = tempFrame;
}

- (IBAction)bottom {
    CGRect tempFrame = self.head.frame;
    tempFrame.origin.y += 10;
    self.head.frame = tempFrame;
}

/**
 *  去掉 autolayout 自动布局
 */
- (IBAction)big
{
    CGRect tempFrame = self.head.frame;
    
    tempFrame.size.width  += 10;
    tempFrame.size.height += 10;
    
    self.head.frame = tempFrame;
}

/**
 *  去掉 autolayout 自动布局
 */
- (IBAction)small
{
    CGRect tempFrame = self.head.frame;
    
    tempFrame.size.width  -= 10;
    tempFrame.size.height -= 10;
    
    self.head.frame = tempFrame;
}


/**
 *  center 属性的使用
 *  UIView 实现 简单动画
 *  @param sender 按钮
 */
- (IBAction)move:(UIButton *)sender {
    
    [UIView beginAnimations:nil context:nil];   //开启简单动画
    [UIView setAnimationDuration:1.0];          //动画持续1秒
    
    
    // 1.取出原来的属性
    CGPoint tempCenter = self.head.center;
    // 2.修改临时属性
    CGFloat delta = 40;
    switch(sender.tag){
        case 1://左
            tempCenter.x -= delta;
            break;
        case 2://上
            tempCenter.y -= delta;
            break;
        case 3://右
            tempCenter.x += delta;
            break;
        case 4://下
            tempCenter.y += delta;
            break;
    }
    // 3.重新赋值
    self.head.center = tempCenter;
    
    
    [UIView commitAnimations];                 //提交动画
}

/**
 *  bounds 属性的使用
 *
 *  @param sender 按钮
 */
- (IBAction)scale:(UIButton *)sender {
    // 1.取出原来的属性
    CGRect tempBounds = self.head.bounds;
    // 2.修改临时属性
    CGFloat delta = 40;
    switch(sender.tag){
        case 5://大
            tempBounds.size.width  += delta;
            tempBounds.size.height += delta;
            break;
        case 6://小
            tempBounds.size.width  -= delta;
            tempBounds.size.height -= delta;
            break;
    }
    // 3.重新赋值
    self.head.bounds = tempBounds;
}

/**
 *  使用frame属性
 *
 *  @param sender 按钮
 */
- (IBAction)moveAndBig:(UIButton *)sender {
    
    CGRect tempFrame = self.head.frame;
    
    tempFrame.origin.x += 10;
    tempFrame.origin.y += 10;
    
    tempFrame.size.width  += 50;
    tempFrame.size.height += 50;
    
    self.head.frame = tempFrame;
}

/**
 *  transform
 */
- (IBAction)up {
    //self.head.transform = CGAffineTransformMakeTranslation(0,-100);
    self.head.transform = CGAffineTransformTranslate(self.head.transform,0,-100);
}

/**
 *  transform
 */
- (IBAction)leftRotate {
    // 1.取得头像按钮
    //通过给子控件绑定tag 获取子控件
    UIButton *head = (UIButton *)[self.view viewWithTag:7];
    
    // 2.向左旋转45°
    // 45  180
    // 角度是正数:顺时针, 角度是负数:逆时针
    //head.transform = CGAffineTransformMakeRotation(-M_PI_4);
    head.transform = CGAffineTransformRotate(head.transform,-M_PI_4);
}

/**
 *  transform
 */
- (IBAction)plus {
    UIButton *head = (UIButton *)[self.view viewWithTag:7];
    //head.transform = CGAffineTransformMakeScale(1.5,1.5);
    head.transform = CGAffineTransformScale(head.transform,1.5,1.5);
}






@end
